package com.example.register_demo.userDatabase

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [UserEntity::class],version = 1)
abstract class UserDatabase : RoomDatabase() {
    //集成Dao对象实现CRUD功能
    abstract fun userDao(): UserDAO
    //使用“Double Check”实现Singleton模式，
    //返回唯一的一个MyDatabase实例给外界使用
    companion object{
        @Volatile
        private var dbInstance: UserDatabase?=null
        fun getDatabase(context: Context): UserDatabase {
            if(dbInstance ==null){
                synchronized(UserDatabase::class.java){
                    if(dbInstance ==null){
                        dbInstance = Room.databaseBuilder(
                            context.applicationContext,
                            UserDatabase::class.java,
                            "appdata.db"
                        ).build()
                    }
                }
            }
            return dbInstance!!
        }
    }
}